﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Services.BookStore;
using Entities.BookStore;

namespace Business.BookStore
{
    public class Store : IBookStoreServiceContract
    {
        private static Books _books;
        public static Books Books
        {
            get
            {
                return _books = _books ?? new Books();
            }
        }
        public ResponseBooks GetBooks()
        {
            var response = new ResponseBooks();
            try
            {
                response.IsOk = true;
                response.Books = Store.Books;
                response.Message = string.Concat("total rows ", Store.Books.Count(), ".");
            }
            catch (Exception ex)
            {

                response.IsOk = false;
                response.Message = ex.Message;
            }
            return response;
        }

        public ResponseBook InsertBook(Book book)
        {
            var response = new ResponseBook();
            try
            {
              //  lock (this)
                {
                    int count = Store.Books.Where(b => b.Title.Equals(book.Title)).Count();
                    bool isExist = count > 0;
                    if (!isExist)
                    {

                        response.IsOk = true;
                       int  c =  Store.Books.Count();
                        book.Id = ++c;
                        Store.Books.Add(book);
                    }
                    else
                    {

                        response.IsOk = false;
                        response.Message = string.Concat(book.Title, " alredy exists.");
                    }
                    response.Book = book;
                }
            }
            catch (Exception ex)
            {
                response.IsOk = false;
                response.Message = ex.Message;

            }
            return response;
        }

        public ResponseBook FindBook(int Id)
        {
            var response = new ResponseBook();
            try
            {
                var _book = Store.Books.Find(new Predicate<Book>(delegate(Book book) { return book.Id == Id; }));
                response.IsOk = true;

                if (_book != null)
                {
                    response.Book = _book;
                }
                else
                {
                    response.Message = string.Concat("book not found  #", Id);
                }

            }
            catch (Exception ex)
            {
                response.IsOk = false;
                response.Message = ex.Message;

            }
            return response;
        }

        public Response UpdateBook(Book book)
        {
            var response = new Response();
            try
            {
                response.IsOk = true;
                var _book = Store.Books.Find(new Predicate<Book>(delegate(Book b)
                {
                    return b.Id == book.Id;
                }));
                if (_book == null)
                    response.Message = string.Concat("book not found.");
                else
                {
                    _book.Title = book.Title;
                    _book.IsActive = book.IsActive;
                    _book.Copies = book.Copies;
                }
            }
            catch (Exception ex)
            {
                response.IsOk = false;
                response.Message = ex.Message;
            }
            return response;
        }

        public Response DeleteBook(int Id)
        {
            var response = new Response();
            try
            {
                response.IsOk = true;
                var _book = Store.Books.Find(new Predicate<Book>(delegate(Book b)
                {
                    return b.Id == Id;
                }));
                if (_book == null)
                    response.Message = string.Concat("book not found.");
                else
                {
                    Store.Books.Remove(_book);
                }
            }
            catch (Exception ex)
            {
                response.IsOk = false;
                response.Message = ex.Message;
            }
            return response;
        }
    }
}
